//体温监测－－－－－工作人员测量体温并上报信息
var app = getApp();
var util = require('../../../utils/util.js');
const outdate = util.formatDate(new Date());
var twvalue, select_fz; ////select_fz存放二维码第一位的值，用于判断是哪个界面的码  0:告知书，1：门诊分诊，2：住院分诊
var name1, lxbx4other, sex1, sfz1, phone1, chulijieguo, zhengzhuang1, lxbx4other1, lxdz, qgnl1, lxbx1, sfwc1, memo1, addtime1, lxbx04zhengzhuang, address1, fromwhere1,  flag, frist_on = "再次登记人员";
var gzry_dep = "", gzry_zqdd = "", gzry_name = "", permission = false, gzryphone, fenzhenMemo, laifangmudi, beifangzhekeshi, beifangzhename,user_openid;   ///存用户原记录openid 用于二次填离开时间
var workaddress, workphone, startwhere, crosswhere, traininfo, train_memo, shiyou; // 下面火车 站信息用
Page({ 
  
  data: {     
    focuss:false,
    mzfz_first:true,//y识别首次填写信息
    zyfz_first:true,
    show: "", 
    baseinformation: false,
    zmyygzs:true, ///告知书view
    zmyyzhuyuanfenzhen: true,
    station_view:true,
    zmyyry_out: true,
    ticket:false,
    hccc: "",  
    tbry: "",
    imga5: ['https://7a6d-zmyy-6uke0-1301213777.tcb.qcloud.la/%E6%88%91%E4%BB%AC%E4%B8%80%E5%AE%9A%E4%BC%9A%E8%83%9C%E5%88%A9.jpg?sign=ff00b2f3195c898cd1c5f25fa3c8329b&t=1581848977'],
    // items: [
    //   { name: '患者告知书', value: '患者告知书' },
    //   { name: '门诊分诊台', value: '门诊分诊台' },
    //   { name: '住院分诊台', value: '住院分诊台' },
    // ],
  
    zmyygzs_zqdd_moda :true,
    zmyymz_fz_tw_moda:true,
    staion_fz_tw_moda:true,
    zmyyzy_fz_tw_moda:true,
  },
  
  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {
    this.findOpenid()
    if (app.globalData.gzry_input_name){
      this.setData({
        returename: "上次填报的人员为：" + app.globalData.gzry_input_name,
        returetwvalue: "  体温：" + app.globalData.gzry_input_twvalue
      })
    }
    app.globalData.gzry_input_name=null
    app.globalData.gzry_input_twvalue=null
  },
  onLoad: function () {
  
  },
  scan_info_go_switch: function (e) {
    const that = this;
    if (permission) {
      this.go_switch();
    }
    else {

      wx.showToast({
        title: '没有权限',
        icon: 'success'
      })

      that.setData({
        tbry: "非登记人员，不能查看！请联系管理人员 15334758875"
      })



    }
  },

  go_switch: function () {
    wx.redirectTo({
      url: '/pages/info/gzry_input/gzry_input'
    })
  },
  scan_info_personinfo: function () {
    wx.navigateTo({
      url: '/pages/gzryinfo/gzryinfo'
    })

  },
  zhuanzhen: function () {
    wx.navigateTo({
      url: '/pages/PersonReported/zhuanzhen_tracer/zhuanzhen_tracer'
    })
   
  },
  myreported: function () {
    wx.navigateTo({
      url: '/pages/PersonReported/PersonReported'
    })

  }, 
  scanCode: function (e) {

    const db = wx.cloud.database();
    wx.scanCode({
      // onlyFromCamera: true,////只允许从相机扫码，去掉可加载相册图片
      success: (res) => {
        // this.show = "结果:" + res.result + "二维码类型:" + res.scanType + "字符集:" + res.charSet + "路径:" + res.path;
        // console.log("二维码成功,携带数据res.result：", res.result)
        // console.log("二维码成功,jieq截取res.result：", res.result.charAt(0))
        // console.log("二维码成功,jieq截取res.result：", res.result.slice(1))
       
        // db.collection('base_zmyy').doc(res.result.slice(1)).get(
        //   select_fz = res.result.charAt(0)

        db.collection('base_zmyy').where({
          _openid: res.result.slice(1)
        }).get(
            select_fz = res.result.charAt(0),
            // console.log("二维码成功,携带数据：", select_fz)


        ).then(res => {
          // res.data 包含该记录的数据
          console.log("二维码成功,携带数据：", res.data[0])
          this.setData({
            returename: "上次填报的人员为：" + res.data[0].name,//提交后提示上次填加人员

            //门诊分诊用
            name1: res.data[0].name,
            sfz1: res.data[0].sfz,
            sex1: res.data[0].sex,
            phone1: res.data[0].phone,
            address1: res.data[0].address,
            fromwhere1: res.data[0].fromwhere,
            lxbx1: res.data[0].lxbx,
            lxbx04zhengzhuang: res.data[0].lxbx04zhengzhuang,
            
            ////tfyr告知书用             
            lxbx4other1: res.data[0].lxbx04qtxx,
            zhengzhuang1: res.data[0].zhengzhuang,
            /////住院分诊用
            laifangmudi: res.data[0].laifangmudi,
            beifangzhekeshi: res.data[0].beifangzhekeshi,
            beifangzhename: res.data[0].beifangzhename,
            //火车站用
            // workaddress: res.data[0].workaddress,
            // workphone: res.data[0].workphone,
            startwhere: res.data[0].startwhere,
            crosswhere: res.data[0].crosswhere,
            traininfo: res.data[0].traininfo,
            train_memo: res.data[0].train_memo,
            shiyou: res.data[0].shiyou

          })
          // console.log("二维码成功,res.result.charAt(0)：", res.result.charAt(0))
          //  //门诊分诊用
          flag = res.data[0].flag,
          name1= res.data[0].name,
            sfz1= res.data[0].sfz,
            sex1= res.data[0].sex,
            phone1= res.data[0].phone,
            address1= res.data[0].address,
            fromwhere1= res.data[0].fromwhere,
            lxbx1= res.data[0].lxbx,
            lxbx04zhengzhuang= res.data[0].lxbx04zhengzhuang,
            ////tfyr告知书用             
            lxbx4other1= res.data[0].lxbx04qtxx,
            zhengzhuang1= res.data[0].zhengzhuang,
            twvalue = "",
             /////住院分诊用
             laifangmudi= res.data[0].laifangmudi,
            beifangzhekeshi= res.data[0].beifangzhekeshi,
            beifangzhename= res.data[0].beifangzhename,
           //火车站用
            workaddress= res.data[0].workaddress,
            workphone= res.data[0].workphone,
            startwhere= res.data[0].startwhere,
            crosswhere= res.data[0].crosswhere,
            traininfo= res.data[0].traininfo,
            shiyou = res.data[0].shiyou,
              train_memo= res.data[0].train_memo,
            /////人员离开用
          user_openid = res.data[0]._openid
          switch (select_fz) {
            case "0":
              this.zmyymz_chlijieguo();
              chulijieguo = ""
              this.setData({
                baseinformation: true,
                zmyygzs: false,
                zmyyzhuyuanfenzhen: true,
                station_view: true,
                inputVal: "",
                fzmemo: "",
                memo1: res.data[0].mz_memo,                
              })
              if (parseInt(flag) == 1) {
               
                this.setData({
                  mzfz_first: false
                })
              }
              break;
            case "1":
              if (res.data[0].shiyou=="买票") {
                this.setData({
                  baseinformation: true,
                  zmyygzs: true,
                  zmyyzhuyuanfenzhen: true,
                  station_view: false,
                  inputVal: "",
                  ticket:true,
                })  
              } else
              {
                this.setData({
                  baseinformation: true,
                  zmyygzs: true,
                  zmyyzhuyuanfenzhen: true,
                  station_view: false,
                  inputVal: "",
                  ticket: false,
                })  
              }
                         
              break;
            case "2":
              this.setData({
                baseinformation: true,
                zmyygzs: true,
                zmyyzhuyuanfenzhen: false,
                station_view: true,
                inputVal: "",
                fzmemo: "",
                memo1: res.data[0].zy_memo,
              })
              if (parseInt(flag) == 1) {                
                this.setData({
                  zyfz_first: false
                })
              }
              break;
          }


        })
          .catch(Error => {
            // console.log("二维码失败wyth：")
            wx.showToast({
              title: '无效信息码',
              icon: 'success',
              duration: 2000
            })
          })



      },
      fail: (res) => {
        // console.log("二维码扫描结果5：", res)
        wx.showToast({
          title: '失败',
          icon: 'success',
          duration: 2000
        })
      },
      complete: (res) => {
        // console.log("二维码扫描失败：", TwoCode )
      }

    })



  },
  zmyymz_chlijieguo: function (e) {

    const db = wx.cloud.database();
    var that = this;
    db.collection('checkbox').doc('hospital_mz_chulijieguo').get({
      success: function (res) {
        // console.log('马上查询数据库，findcheckbox为:', res.data.chulijieguo)
        that.setData({
          hospital_mz_chulijieguo: res.data.chulijieguo,
        })
      }
    })
  },
  chulijieguo: function (e) {
    chulijieguo =e.detail.value
    // console.log('chulijieguo result: ', chulijieguo) 
  },
  findOpenid: function () {
    var that =this;
    const db = wx.cloud.database();
    wx.cloud.callFunction({
      name: 'login',
      complete: res => {
        // console.log('onGetOpenid result: ', app.globalData.openid)
        app.globalData.openid = res.result.openid

        db.collection('glry').where({
          _openid: app.globalData.openid
        })
          .get()
          .then(res => {
            // console.log('马上查询数据库，opneid为:', app.globalData.openid)
            if (res.data.length) {
              // wx.showToast({
              //   title: '已添加过'
              // })
              // console.log('已添加过', res.data)
              // console.log('已添加过', res.data[0]._id)
              this.setData({
                tbry: "填报人员为 " + res.data[0].dep + " 的: " + res.data[0].name,
                gzry_zqdd: res.data[0].zqaddress,
                gzry_name: res.data[0].name,
                // gzry_name: res.data[0].姓名,
                // name_phone: res.data[0].电话,               
              })
              // personid = res.data[0]._id
              gzry_dep = res.data[0].dep
              gzry_zqdd = res.data[0].zqaddress
              gzry_name = res.data[0].name
              gzryphone = res.data[0].gzryphone
              permission = res.data[0].ruler
              // name_phone = res.data[0].电话
            }
            else {
              // console.log('没e有人员信息', res.data)
              // console.log('this.data.modalHidden1', this.data.modalHidden1)
              this.setData({
                tbry: "请到 服务中心-->工作人员绑定 后再使用扫码功能！",
                zmyygzs_zqdd_moda: false,
              })
               
            }
          })
      }
    })


  },
  //事件处理函数
  // bindViewTap: function () {
  //   this.setData({
  //     modalHidden: !this.data.modalHidden
  //   })

  // },
 
    //确定按钮点击事件,告知书复核
  zmyygzs_fh_en: function () {
    this.setData({
      mzfz_first: true,
      zyfz_first :true
    })
    frist_on = "首次登记人员"
  },
    //取消按钮点击事件,,告知书复核
  zmyygzs_fh_ca: function () {
    this.setData({
      mzfz_first: true,
      zyfz_first: true
    })
    frist_on = "首次登记人员"
  },
  //确定按钮点击事件,确认值勤信息
  zmyygzs_zqdd_en: function () {
    wx.navigateTo({
      url: '/pages/gzryinfo/gzryinfo'
    })
    this.setData({
      zmyygzs_zqdd_moda: true
    })
  },
  //取消按钮点击事件,确认值勤信息
  zmyygzs_zqdd_ca: function () {
   
    this.setData({
      zmyygzs_zqdd_moda: true
    })
  },
  //确定按钮点击事件,门诊体温
  zmyymz_tw_en: function () {
    // console.log('确定按钮点击事件,门诊体温') 
    this.add_mzfz();
    this.setData({
      zmyymz_fz_tw_moda: true
    })
  },
  //取消按钮点击事件,门诊体温
  zmyymz_tw_ca: function () {
    // console.log('取消按钮点击事件,门诊体温：') 
    // wx.navigateTo({
    //   url: '/pages/gzryinfo/gzryinfo'
    // })
    this.setData({
      zmyymz_fz_tw_moda: true
    })
  },
  station_tw_en: function () {
    // console.log('确定按钮点击事件,门诊体温') 
    this.add_station();
    this.setData({
      staion_fz_tw_moda: true,
    })
  },
  //取消按钮点击事件,o火车站
  station_tw_ca: function () {
   
    this.setData({
      staion_fz_tw_moda: true,
    })
  },
  //确定按钮点击事件,住院体温
  zmyyzy_tw_en: function () {
    // console.log('确定按钮点击事件,门诊体温') 
    this.add_zyfz();
    this.setData({
      zmyyzy_fz_tw_moda: true
    })
  },
  //取消按钮点击事件,住院体温
  zmyyzy_tw_ca: function () {
    // console.log('取消按钮点击事件,门诊体温：') 
    // wx.navigateTo({
    //   url: '/pages/gzryinfo/gzryinfo'
    // })
    this.setData({
      zmyyzy_fz_tw_moda: true
    })
  },
  scan_info: function (e) {    
    const that = this;
    if ( permission) {
      this.scanCode();      
    }
    else {
      
        wx.showToast({
          title: '没有权限',
          icon: 'success'
        })
        
          that.setData({
            tbry: "扫码功能需要管理员审核后方可使用！请联系管理人员 15334758875"
          })
        
      
      
    }
  },
  scan_test: function (e) {
   
      this.scanCode();
   
  },
  scan_info_zhuanzhen: function (e) {
    const that = this;
    if (permission) {
      this.zhuanzhen();
    }
    else {
      wx.showToast({
        title: '没有权限',
        icon: 'success'
      })

      that.setData({
        tbry: "非登记人员，不能查看！请联系管理人员 15334758875"
      })



    }
  },
  scan_info_shangbaomesssage: function (e) {
    const that = this;
    if (permission) {
      this.myreported();
    }
    else {

      wx.showToast({
        title: '没有权限',
        icon: 'success'
      })

      that.setData({
        tbry: "非登记人员，不能查看！请联系管理人员 15334758875"
      })



    }
  },
  
  
 
  add_station: function (e) {
    const db = wx.cloud.database()    
      this.setData({
        baseinformation: false,   ////扫描页面
        zmyygzs: true, //告知书信息页面
        zmyyzhuyuanfenzhen: true,
        station_view: true,
        // inputVal: "",     //体温 输入 框
        // fzmemo: "",
      })
      db.collection('info_zmyy').add({
        // data 传入需要局部更新的数据
        data: {

          // 表示将 done 字段置为 true 
          name: name1,
          sex: sex1,
          sfz: sfz1,
          phone: phone1,
          address: address1,
          fromwhere: fromwhere1,
          lxbx: lxbx1,
          lxbx04zhengzhuang: lxbx04zhengzhuang,
          lxbx4other1: lxbx4other1,         
          form: "火车站测温台",
          input: "自己录入",
          gzryphone: gzryphone,
          gzry_addtime: util.formatTime(new Date()),
          gz_address: gzry_zqdd,
          gzry_dep: gzry_dep,
          gzry_name: gzry_name,
          twvalue: twvalue,
          gzry_memo: fenzhenMemo,
          shiyou: shiyou,
          workaddress: workaddress,
          workphone: workphone,
          startwhere: startwhere,
          crosswhere: crosswhere,
          traininfo: traininfo,
          train_memo: train_memo,
        },
        success: function (res) {
          wx.showToast({
            title: '人员上报成功',
            icon: 'success',
          })
        }
      })

    
    
  }
  ,    

  zmyymz_twvalue_check : function (e) {
    if (parseFloat(twvalue) > 34 && parseFloat(twvalue) < 41) {
      // console.log("输出1：", parseFloat(twvalue))
      if (parseFloat(twvalue) >= 37) {
        switch (select_fz) {

          case "0":
            this.setData({
              zmyymz_fz_tw_moda: false 
            })
            break;
          case "1":
            this.setData({
              staion_fz_tw_moda: false
            })
            break;
          case "2":
            this.setData({
              zmyyzy_fz_tw_moda: false
              // inputVal: "",
            })
            break;
        }
      }
      else {
        // console.log("体温正常", parseFloat(twvalue))
                    switch (select_fz) {
                     
                      case "0":
                        this.add_mzfz();
                        this.setData({
                          zmyymz_fz_tw_moda: true
                          // inputVal: "",
                        })
                        break;
                      case "1":
                        this.add_station();
                        this.setData({
                          staion_fz_tw_moda: true
                          // inputVal: "",
                        })
                        break;
                      case "2":
                       this.add_zyfz();
                        this.setData({
                          zmyyzy_fz_tw_moda: true
                          // inputVal: "",
                        })
                        break;
                    }
       
      }
    }
    else {
      // console.log("不在范围", parseFloat(twvalue))
      wx.showToast({
        title: '体温不对请重测',
        icon: 'success'
      })
    } 
  },
  add_mzfz: function (e) {
    const db = wx.cloud.database()   
    if (chulijieguo) {
    this.setData({
      baseinformation: false,   ////扫描页面
      zmyygzs: true, //告知书信息页面
      zmyyzhuyuanfenzhen:true,
      station_view: true,
      // inputVal: "",     //体温 输入 框
      // fzmemo: "",
    })
    db.collection('info_zmyy').add({
      // data 传入需要局部更新的数据
      data: {
        
        // 表示将 done 字段置为 true 
        name: name1,
        sex:sex1,
        sfz:  sfz1,
        phone:  phone1,
        address:  address1,
        fromwhere:  fromwhere1,
        lxbx:  lxbx1,
        lxbx04zhengzhuang: lxbx04zhengzhuang,
        lxbx4other1: lxbx4other1,
        zhengzhuang1: zhengzhuang1,
        mz_memo:  memo1,
        form: "门诊分诊台",
        input: "自己录入",
        gzryphone:  gzryphone,
        gzry_addtime: util.formatTime(new Date()),
        gz_address:  gzry_zqdd,
        gzry_dep:  gzry_dep,
        gzry_name:  gzry_name,
        twvalue:  twvalue,
        chulijieguo: chulijieguo,
        gzry_memo: fenzhenMemo,
        user_openid: user_openid,
        outtime:"",
        arrived: "未抵达",
        zzwhere:"",
        fhtwvalue:"",
        zzmemo:""   ,
        zz_gzry:"" ,   
        frist_on: frist_on
      },
      success: function (res) {
        wx.showToast({
          title: '人员上报成功',
          icon: 'success',
        })
      }
    })
     
    }
    else {
      wx.showToast({
        title: '请选择处理结果',
        icon: 'success',
      })
    }
  }
  ,    
  add_zyfz: function (e) {
    const db = wx.cloud.database()
    this.setData({
      baseinformation: false,   ////扫描页面
      zmyygzs: true, //告知书信息页面
      zmyyzhuyuanfenzhen: true,
      // inputVal: "",     //体温 输入 框
      // fzmemo: "",
    })
    db.collection('info_zmyy').add({
      // data 传入需要局部更新的数据
      data: {

        // 表示将 done 字段置为 true 
        name: name1,
        sfz: sfz1,
        phone: phone1,
        hz_info_time: addtime1,
        zy_memo: memo1,
        form: "住院分诊台",
        input: "自己录入",
        gzryphone: gzryphone,
        gzry_addtime: util.formatTime(new Date()),
        gz_address: gzry_zqdd,
        gzry_dep: gzry_dep,
        gzry_name: gzry_name,
        twvalue: twvalue,
        laifangmudi: laifangmudi,
        beifangzhekeshi: beifangzhekeshi,
        beifangzhename: beifangzhename,
        gzry_memo: fenzhenMemo,
        outtime:"",
        user_openid: user_openid,
        frist_on: frist_on
      },
      success: function (res) {
        wx.showToast({
          title: '人员上报成功',
          icon: 'success',
        })
      }
    })

  }
  ,   
  

  onInputEvent: function (e) {
    twvalue = e.detail.value
    this.setData({
      returetwvalue: "  体温：" + twvalue
    })
  },
  onInputFenzhenMemo: function (e) {
    fenzhenMemo = e.detail.value
    
  },


})
